package com.fulong.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fulong.dao.Status;
import com.fulong.entity.UserBaiseInfoEntity;
import com.fulong.entity.UserMsgInfoEntity;
import com.fulong.feign.RemtoServlet;
import com.fulong.service.UserBaiseInfoService;
import com.fulong.service.UserMsgInfoService;
import com.fulong.service.Userservice;
import com.fulong.web.webconfig.UserStatus;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fulong.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Controller
public class UserController {

    @Autowired
    Userservice userservice;

    @Autowired
    UserBaiseInfoService userBaiseInfoService;
//    @Autowired
//    RemtoServlet remtoServlet;
    /*
    * 登陆功能 ，多账号可同时登陆
    * */
    @ResponseBody
    @GetMapping("/userlogin")
    public R userLogin(@RequestParam("userid") String userid ,
                         @RequestParam("userpwd") String userpwd ,
                         HttpServletRequest request) {
        Map<String, Object> map = userservice.qureyUerLogin(userid);
        if(!map.get("pwd").equals(userpwd)) {
            //验证账号正确性
            return R.error();
        }
        //登陆
        request.getSession().setAttribute(UserStatus.LOGIN_STATUS , map.get("id"));
        //返回状态
        UserBaiseInfoEntity userinfo = userBaiseInfoService.getOne(new QueryWrapper<UserBaiseInfoEntity>().eq("account" , userid));
        return R.ok().put("info",userinfo);
    }
    /*
    * 修改密码 ， 暂时不需要验证信息
    * */
    @ResponseBody
    @GetMapping("/alterpwd")
    public int userAlterpwd (
            @RequestParam("newpwd") String newpwd){
        boolean b = userBaiseInfoService.update(new UpdateWrapper<UserBaiseInfoEntity>().eq("id", UserStatus.getParam().getId()).set("pwd", newpwd));
        if(!b){
            return Status.login_status.faile.ordinal();
        }
        return Status.login_status.success.ordinal() ;
    }

    /*
    * 远程调用测试
    * */
//    @ResponseBody
//    @GetMapping("/remote")
//    public String remoteTest (){
//        System.out.println("调用");
//        return remtoServlet.remtoServletTest();
//    }
}
